/* ==================================================================   
 * Created 2013-10-27下午9:43:13 by 郑伟
 * ==================================================================  
 *  EMV  Level2
 * Copyright (c) SZZCS S&T Co.ltd ShenZhen, 2012-2013 
 * ================================================================== 
 * 深圳市中磁计算机技术有限公司拥有该文件的使用、复制、修改和分发的许可权
 * 如果你想得到更多信息，请访问 <http://www.szzcs.com/cn>
 *
 * SZZCS S&T Co.ltd ShenZhen owns permission to use, copy, modify and 
 * distribute this documentation.
 * For more information on EMV, please 
 * see <http://www.szzcs.com/cn>.  
 * ================================================================== 
 */
package com.hangup.util;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.devil.bbsclient.R;
import com.hangup.net.ActivityKeys;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.view.View;

public class NetWorkUtil {
	// 判断是否联网
	public static String NetType(final Context context) {
		try {
			ConnectivityManager cm = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo info = cm.getActiveNetworkInfo();
			String typeName = info.getTypeName().toLowerCase(); // WIFI/MOBILE
			if (typeName.equalsIgnoreCase("wifi")) {
			} else {
				typeName = info.getExtraInfo().toLowerCase();
				// 3gnet/3gwap/uninet/uniwap/cmnet/cmwap/ctnet/ctwap
			}
			return typeName;
		} catch (Exception e) {
			return null;
		}
	}

	// 天气预报图片获取
	/**
	 * @author Administrator
	 * @param param
	 *            -图片字符串
	 */

	public static int getImage(String param) {
		int currentid = 0;
		String[] arr = param.split("\\.");
		//
		if (param.length() > 2) {
			return 0;
		}
		//
		int tag;
		if (arr[0].equals("nothing")) {
			tag = 0;
		} else {
			tag = Integer.parseInt(arr[0]);
		}

		switch (tag) {
		case 0:
			currentid = R.drawable.w01;
			break;
		case 1:
			currentid = R.drawable.w02;
			break;
		case 2:
			currentid = R.drawable.w03;
			break;
		case 3:
			currentid = R.drawable.w04;
			break;
		case 4:
			currentid = R.drawable.w05;
			break;
		case 5:
			currentid = R.drawable.w06;
			break;
		case 6:
			currentid = R.drawable.w07;
			break;
		case 7:
			currentid = R.drawable.w08;
			break;
		case 8:
			currentid = R.drawable.w09;
			break;
		case 9:
			currentid = R.drawable.w10;
			break;
		case 10:
			currentid = R.drawable.w11;
			break;
		case 11:
			currentid = R.drawable.w12;
			break;
		case 12:
			currentid = R.drawable.w13;
			break;
		case 13:
			currentid = R.drawable.w14;
			break;
		case 14:
			currentid = R.drawable.w15;
			break;
		case 15:
			currentid = R.drawable.w16;
			break;
		case 16:
			currentid = R.drawable.w17;
			break;
		case 17:
			currentid = R.drawable.w18;
			break;
		case 18:
			currentid = R.drawable.w19;
			break;
		case 19:
			currentid = R.drawable.w20;
			break;
		case 20:
			currentid = R.drawable.w21;
			break;
		case 21:
			currentid = R.drawable.w22;
			break;
		case 22:
			currentid = R.drawable.w23;
			break;
		case 23:
			currentid = R.drawable.w24;
			break;
		case 24:
			currentid = R.drawable.w25;
			break;
		case 25:
			currentid = R.drawable.w26;
			break;
		case 26:
			currentid = R.drawable.w27;
			break;
		case 27:
			currentid = R.drawable.w28;
			break;
		case 28:
			currentid = R.drawable.w29;
			break;
		case 29:
			currentid = R.drawable.w30;
			break;
		case 30:
			currentid = R.drawable.w31;
			break;
		case 31:
			currentid = R.drawable.w32;
			break;
		case 32:
			currentid = R.drawable.w33;
			break;
		default:
			currentid = R.drawable.w01;
			break;

		}
		return currentid;
	}

	// 显示对话框
	public static Dialog showDialog(Context context, View view, String title,
			String content) {
		Dialog alertDialog = new AlertDialog.Builder(context).setTitle(title)
				.setMessage(content).create();
		return alertDialog;
	}

	// 验证是否连接到服务器
	public static boolean webServer() {
		boolean isConn = false;
		URL url;
		HttpURLConnection conn = null;
		try {
			url = new URL(ActivityKeys.BASE_URL);
			conn = (HttpURLConnection) url.openConnection();
			conn.setConnectTimeout(1000 * 5);
			if (conn.getResponseCode() == 200) {
				isConn = true;
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			conn.disconnect();
		}
		return isConn;
	}

}
